0% completed
Problem Statement
Given an integer array coins, where coins[i] = x represents the coin of amount x, and integer amount, return the total number of distinct ways to make a amount of money using a given set of coin denominations. If that amount of money cannot be made up by any combination of the coins, return 0.
You can have an infinite number of each coin.
Examples
-
Example 1:
- Input:
amount = 4, coins = [1,2,3] - Expected Output:
4 - Justification: The four ways to make 4 are:
[1,1,1,1],[1,1,2],[2,2], and[1,3].
- Input:
-
Example 2:
- Input:
amount = 10, coins = [2, 5, 3, 6] - Expected Output:
5 - Justification: The five ways to make 10 are:
[2,2,2,2,2],[2,2,3,3],[5,5],[2,2,6], and[2,3,5]`.
- Input:
-
Example 3:
- Input:
amount = 0, coins = [7,3] - Expected Output:
1 - Justification: There is only one way to make 0, which is by not using any coins at all.
- Input:
Solution
To solve this problem, we'll use dynamic programming because it allows us to break down the problem into smaller, manageable subproblems, each of which can be solved just once and then stored for future reference. This approach is effective because it eliminates the need for redundant calculations, thus optimizing performance.
We'll create a table (array) where each entry represents the number of ways to make up an amount using the available denominations up to that point. Starting with the base case where the amount is 0 (which has 1 way, using no coins), we progressively calculate the number of ways for all amounts up to the target, including it.
Our strategy ensures efficiency and scalability, capable of handling a wide range of inputs. It is designed to systematically explore all combinations of coin usage, dynamically building upon the solutions of smaller amounts to reach the final target. This methodology not only guarantees that all possible combinations are considered but also leverages previously computed results for rapid computation of subsequent solutions.
Step-by-Step Algorithm
-
Initialization: Create an array
dpof lengthamount + 1and initialize all its elements to 0. This array will hold the number of ways to make each amount from 0 toamount. Setdp[0] = 1because there is exactly one way to make an amount of 0, which is by using no coins. -
Iterate Over Coins: Loop through each coin in the
coinsarray. For each coin, proceed to the next step. -
Update DP Array: For the current coin, iterate over the
dparray starting from the index equal to the coin's value up toamount. For each indexi, updatedp[i]by adding the value ofdp[i - coin]to it. This step effectively adds the number of ways to make the amountiby including the current coin to the number of waysicould be made without it. -
Repeat: Repeat the above step for each coin in the coins array.
-
Result: After iterating through all coins,
dp[amount]will contain the total number of ways to make the target amount using the given coins.
Algorithm Walkthrough
Given amount = 10 and coins = [2, 5, 3, 6], let's walk through the algorithm step-by-step:
Initial Setup
amount = 10coins = [2, 5, 3, 6]- Initial
dparray:[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Processing Coin 2:
-
Update for
amount = 2:dp[2] += dp[2 - 2]dparray becomes[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
-
Update for
amount = 3:dp[3] += dp[3 - 2]dparray remains the same.
-
Update for
amount = 4:dp[4] += dp[4 - 2]dparray becomes[1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
-
Update for
amount = 5:dp[5] += dp[5 - 2]dparray remains the same.
-
Update for
amount = 6:dp[6] += dp[6 - 2]dparray becomes[1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0]
-
Update for
amount = 7:dp[7] += dp[7 - 2]dparray remains the same.
-
Update for
amount = 8:dp[8] += dp[8 - 2]dparray becomes[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0]
-
Update for
amount = 9:dp[9] += dp[9 - 2]dparray remains the same.
-
Update for
amount = 10:dp[10] += dp[10 - 2]dparray becomes[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
Processing Coin 5:
-
Update for
amount = 5:dp[5] += dp[5 - 5]dparray becomes[1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1]
-
Update for
amount = 6:dp[6] += dp[6 - 5]dparray remains the same.
-
Update for
amount = 7:dp[7] += dp[7 - 5]dparray becomes[1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1]
-
Update for
amount = 8:dp[8] += dp[8 - 5]dparray becomes[1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1]
-
Update for
amount = 9:dp[9] += dp[9 - 5]dparray becomes[1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1]
-
Update for
amount = 10:dp[10] += dp[10 - 5]dparray becomes[1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 2]
Processing Coin 3:
-
Update for
amount = 3:dp[3] += dp[3 - 3]dparray becomes[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2]
-
Update for
amount = 4:dp[4] += dp[4 - 3]dparray becomes[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2]
-
Update for
amount = 5:dp[5] += dp[5 - 3]dparray becomes[1, 0, 1, 1, 1, 2, 1, 1, 1, 1, 2]
-
Update for
amount = 6:dp[6] += dp[6 - 3]dparray becomes[1, 0, 1, 1, 1, 2, 2, 1, 1, 1, 2]
-
Update for
amount = 7:dp[7] += dp[7 - 3]dparray becomes[1, 0, 1, 1, 1, 2, 2, 2, 1, 1, 2]
-
Update for
amount = 8:dp[8] += dp[8 - 3]dparray becomes[1, 0, 1, 1, 1, 2, 2, 2, 3, 1, 2]
-
Update for
amount = 9:dp[9] += dp[9 - 3]dparray becomes[1, 0, 1, 1, 1, 2, 2, 2, 3, 3,2]
-
Update for
amount = 10:dp[10] += dp[10 - 3]dparray remains[1, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4]
Processing Coin 6:
-
Update for
amount = 6:dp[6] += dp[6 - 6]dparray becomes[1, 0, 1, 1, 1, 2, 3, 2, 3, 3, 4]
-
Update for
amount = 7:dp[7] += dp[7 - 6]dparray becomes[1, 0, 1, 1, 1, 2, 3, 2, 3, 3, 4]
-
Update for
amount = 8:dp[8] += dp[8 - 6]dparray becomes[1, 0, 1, 1, 1, 2, 3, 2, 4, 3, 4]
-
Update for
amount = 9:dp[9] += dp[9 - 6]dparray becomes[1, 0, 1, 1, 1, 2, 3, 2, 4, 4, 4]
-
Update for
amount = 10:dp[10] += dp[10 - 6]dparray becomes[1, 0, 1, 1, 1, 2, 3, 2, 4, 4, 5]
Return value of dp[amount], which is 5.
Code
Complexity Analysis
Time Complexity
: Here, mis the amount andnis the number of coin denominations. The time complexity arises because we iterate over each coin denomination and for each coin, we iterate over all amounts from the coin's value up tom.
Space Complexity
: The space complexity is linear with respect to the amount mbecause we maintain a dynamic programming table (dparray) of sizem + 1to store the number of ways to make each amount up tom.
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity